package com.dd;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.chantel.tool.db.DBUtil;
import com.dd.exception.UserNotFoundException;
import com.dd.exception.XiangMuNotFoundException;

public class XiangMuManager {
	private static final Logger logger = Logger.getLogger(XiangMuManager.class
			.getName());

	//
	// 通过用户的ID返回和用户相关的项目
	//
	public static ArrayList GetUserManageedXiangMuByUserID(int userID) {
		/*
		 * 效率低 String selectCommand = " SELECT XiangMuTable.* " + " FROM
		 * XiangMuTable RIGHT OUTER JOIN " + "YuanGongXiangMuTable ON " +
		 * "XiangMuTable.XiangMuId = YuanGongXiangMuTable.XiangMuId " + "WHERE
		 * (YuanGongXiangMuTable.YuanGongId = " + ((Integer)(new
		 * Integer(userID))).toString() + " )";
		 */
		logger.debug("GetUserManageedXiangMuByUserID(int userID):userid="
				+ userID);
		String selectCommand = "SELECT XiangMuTable.* " + "FROM XiangMuTable "
				+ "where XiangMuTable.XiangMuId "
				+ " in  (select  YuanGongXiangMuTable.XiangMuId "
				+ " from YuanGongXiangMuTable "
				+ " where YuanGongXiangMuTable.YuanGongId = "
				+ ((Integer) (new Integer(userID))).toString() + ")";
		logger.debug("sql:" + selectCommand);
		DBUtil du = DBUtil.getInstance();

		ArrayList aL = new ArrayList();

		List l = du.getRecords(selectCommand);

		Iterator it = l.iterator();
		while (it.hasNext()) {
			Map m = (Map) it.next();

			int id = ((Integer) m.get("XiangMuId")).intValue();
			String beiZhu = (String) m.get("BeiZhu");
			String mingCheng = (String) m.get("MingCheng");
			String jianCheng = (String) m.get("JianCheng");

			String zhuangTai = (String) m.get("ZhuangTai");

			Date beginTime = (Date) m.get("KaiShiShiJian");
			int nDengDaiShuMu = ((Integer) m.get("DengDaiShenPiShuMu"))
					.intValue();
			int nBiaoDanZhongshu = ((Integer) m.get("ShenPiZhongShuMu"))
					.intValue();
			int nBiaoDanTongGuoShu = ((Integer) m.get("ShenPiJuJueShuMu"))
					.intValue();
			int nBiaoDanJuJueShu = ((Integer) m.get("ShenPiJuJueShuMu"))
					.intValue();

			logger.debug(new Integer(id));
			logger.debug(mingCheng);
			logger.debug(jianCheng);
			logger.debug(new Float(nDengDaiShuMu));
			logger.debug(new Integer(nBiaoDanZhongshu));
			logger.debug(beginTime);



			XiangMu xm = new XiangMu();
			xm.setId(id);
			xm.setBeiZhu(beiZhu);
			xm.setJianCheng(jianCheng);
			xm.setMingCheng(mingCheng);
			xm.setZhuangTai(zhuangTai);
			xm.setKaiShiShiJian(beginTime);
			xm.setShenPiJuJueShuMu(nBiaoDanJuJueShu);
			xm.setShenPiZhongShuMu(nBiaoDanZhongshu);
			xm.setShenPiTongGuoShuMu(nBiaoDanTongGuoShu);
			xm.setDenDaiShengPiShuMu(nDengDaiShuMu);

			aL.add(xm);

		}

		return aL;
	}

	public static ArrayList GetXiangMusByXiangMuShiYeId(int XiangMuShiYeID) {
		DBUtil du = DBUtil.getInstance();

		ArrayList aL = new ArrayList();
		String sql = "select* from XiangMuTable where XiangMuShiYeBuID = "
				+ ((Integer) (new Integer(XiangMuShiYeID))).toString();
		;
		List l = du.getRecords(sql);

		Iterator it = l.iterator();
		while (it.hasNext()) {
			Map m = (Map) it.next();

			XiangMu xm = new XiangMu(m);
			aL.add(xm);
		}

		return aL;
	}

	public static String getXiangMuHtmlOption(int userid) {
		ArrayList a = GetUserManageedXiangMuByUserID(userid);
		StringBuffer xianMu_html_option = new StringBuffer();
		Iterator it = a.iterator();
		logger.debug("getXiangMuHtmlOption() get size:" + a.size());
		while (it.hasNext()) {
			XiangMu xm = (XiangMu) it.next();

			if (xm != null) {
				xianMu_html_option.append("<option value=\"" + xm.getId()
						+ "\">" + xm.getMingCheng() + "</option>");
			}
		}
		return xianMu_html_option.toString();
	}

	public static XiangMu getxiangMuByID(int id) {
		String sql = "select * from XiangMuTable";
		Map whereFields = new HashMap();
		whereFields.put("XiangMuId", new Integer(id));
		DBUtil2 db2 = new DBUtil2();
		Map map = db2.getRow(sql,whereFields);

		int shenPiZhongShuMu, shenPiTongGuoShuMu, shenPiJuJueShuMu;
		Integer temp = (Integer) map.get("ShenPiZhongShuMu");
		if (temp == null)
			shenPiZhongShuMu = -1;
		else
			shenPiZhongShuMu = temp.intValue();

		temp = (Integer) map.get("ShenPiTongGuoShuMu");
		if (temp == null)
			shenPiTongGuoShuMu = -1;
		else
			shenPiTongGuoShuMu = temp.intValue();

		temp = (Integer) map.get("ShenPiJuJueShuMu");
		if (temp == null)
			shenPiJuJueShuMu = -1;
		else
			shenPiJuJueShuMu = temp.intValue();

		XiangMu xm = new XiangMu((String) map.get("MingCheng"), (String) map
				.get("BeiZhu"), (String) map.get("ZhuangTai"),
				(java.util.Date) map.get("KaiShiShiJian"), (java.util.Date) map
						.get("JieShuShiJian"), (String) map.get("JianCheng"),
				shenPiZhongShuMu, shenPiTongGuoShuMu, shenPiJuJueShuMu);
		xm.setId(id);
		return xm;
	}

	//采购表信息:
	private static int get_CGBI_Count(int projId,String strZhuangTai)
	  {
			DBUtil du = DBUtil.getInstance();

			ArrayList aL = new ArrayList();
			String sql = "select* from CaiGouBiaoTable where XiangMuId = " 
						+ ((Integer) (new Integer(projId))).toString()
						+ " and BiaoZhuangTai = " + strZhuangTai;
			List l = du.getRecords(sql);
			int nCount = 0;
			nCount = l.size();

		  return nCount; 
	  }
	//
	//返回项目等待审核的数目
	//
	public static int getDengDai_CGBJ_CountByProjectID(int projectId)
	{
		  int nCount = 0;
		  nCount = XiangMuManager.get_CGBI_Count(projectId, "0");

		  return nCount;
	}
	//
	//返回审核通过的表单数
	//
	public static int getTongGuo_CGBJ_CountByProjectID(int projectId)
	{
		  int nCount = 0;
		  nCount = XiangMuManager.get_CGBI_Count(projectId, "1");

		  return nCount;
	}
	//
	//返回审核拒绝的表单数
	//
	public static int getJuJue_CGBJ_CountByProjectID(int projectId)
	{
		  int nCount = 0;
		  nCount = XiangMuManager.get_CGBI_Count(projectId, "2");

		  return nCount;
	}
	//
	//返回总的表单数
	//
	public static int getTotal_CGBJ_CountByProjectID(int projectId)
	{
		DBUtil du = DBUtil.getInstance();

		ArrayList aL = new ArrayList();
		String sql = "select* from CaiGouBiaoTable where XiangMuId = " 
					+ ((Integer) (new Integer(projectId))).toString();
		List l = du.getRecords(sql);
		int nCount = 0;
		nCount = l.size();

		return nCount; 
	}
	
	public static void main(String[] args) {

		XiangMuManager.GetUserManageedXiangMuByUserID(7);

	}

}
